import Qs from "qs";
import {postAction} from '@/api/manage'

var lastTime = new Date().getTime();
var currentTime = new Date().getTime();
var timeOut = 30 * 60 * 1000; //设置超时时间： 30分

window.onload = function () {
    window.document.onmousedown = function () {
        localStorage.setItem("lastTime",new Date().getTime());
    }
};
function checkTimeout() {
    currentTime = new Date().getTime(); //更新当前时间
    lastTime = localStorage.getItem("lastTime");
    // console.log(currentTime - lastTime);
    // console.log(timeOut);
    if (currentTime - lastTime > timeOut) { //判断是否超时
        // console.log("超时");
        // var url = window.location.href;
        // var newUrl=url.match(/(\S*)#/)[1];
        // window.open(newUrl + '#/login','_self');
        user_logout();
        localStorage.removeItem("token");
        localStorage.removeItem("lastTime");
        window.history.go(0);

    }
}

//调用后台注销登录
function user_logout() {
    let autohor_token = localStorage.getItem("token");
    let params = {
        token:autohor_token
    };
    postAction(domain.publicUrl+"/user/interface/web_user_logout", Qs.stringify(params)).then(res => {
        let result = res.data;
        if (result.code==0) {
            this.$message.success("您因长时间未操作，已自动退出登录")
        } else {
            this.$message.error(result.msg)
        }
    })
}

/* 定时器 间隔30秒检测是否长时间未操作页面 */
window.setInterval(checkTimeout, 30000);

